package com.kekezu.kppw.data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.bean.TestEvent;
import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;
import com.kekezu.kppw.utils.StrFormat;

@SuppressWarnings("deprecation")
public class HireData {
	static FinalDb db;
	static Intent intent;

	/**
	 * 创建雇佣信息
	 * 
	 * @param strTitle
	 *            标题
	 * @param strDesc
	 *            详情
	 * @param strPhone
	 *            联系方式
	 * @param strDelivery
	 *            截止时间
	 * @param strBounty
	 *            预算
	 * @param strUid
	 *            被雇佣人id
	 * @param strFile
	 *            附件
	 * @param strServiceId
	 *            雇佣服务id
	 * @param context
	 * @return
	 */
	public static Map<String, String> createEmploy(String strTitle,
			String strDesc, String strPhone, String strDelivery,
			String strBounty, String strUid, String strFile,
			String strServiceId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/createEmploy";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("title", strTitle));
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("phone", strPhone));
		params.add(new BasicNameValuePair("delivery_deadline", strDelivery));
		params.add(new BasicNameValuePair("bounty", strBounty));
		params.add(new BasicNameValuePair("employee_uid", strUid));
		params.add(new BasicNameValuePair("file_id", strFile));
		params.add(new BasicNameValuePair("service_id", strServiceId));

		Log.e("strFile", strFile);

		try {
			String strJson = httpUtils.httpPost(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				resMap.put("code", resCode);
				resMap.put("msg", resMsg);
				resMap.put("employ_id", jsondata.getString("employ_id"));
				resMap.put("order_id", jsondata.getString("order_id"));
			} else {
				JSONArray jsondata = new JSONArray(jsonObject.getString("data"));
				resMap.put("code", resCode);
				resMap.put("msg", jsondata.get(0).toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 雇佣，赏金托管
	 * 
	 * @param orderId
	 * @param employId
	 * @param intType
	 * @param strPwd
	 * @param context
	 */
	public static Map<String, String> cashPayEmploy(String orderId,
			String employId, int intType, String strPwd, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/cashPayEmploy";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("order_id", orderId));
		params.add(new BasicNameValuePair("employ_id", employId));
		params.add(new BasicNameValuePair("pay_type", "" + intType));
		params.add(new BasicNameValuePair("password", strPwd));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			resMap.put("code", resCode);
			resMap.put("msg", resMsg);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return resMap;
	}

	/**
	 * 雇佣，第三方支付
	 * 
	 * @param strTitle
	 * @param strCash
	 * @param strType
	 * @param strTaskId
	 * @param context
	 * @return
	 */
	public static String ThirdCashEmployPay(String orderId, String employId,
			String strType, Context context) {
		String strRes = null;
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/ThirdCashEmployPay";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("order_id", orderId));
		params.add(new BasicNameValuePair("employ_id", employId));
		params.add(new BasicNameValuePair("pay_type", strType));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()
				.toString()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);

			Log.e("strJson", strJson);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				if (strType.equals("alipay")) {
					strRes = jsondata.getString("payParam").toString();
				} else {
					strRes = jsondata.getString("params").toString();
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return strRes;
	}

	/**
	 * 获取雇佣订单详情信息
	 * 
	 * @param strId
	 *            雇佣id
	 * @param context
	 * @return
	 */
	public static Map<String, String> employDetail(String strId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("employ_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				resMap.put("data", jsonObject.getString("data"));
				resMap.put("employ_att", jsonObject.getString("employ_att"));
				resMap.put("comment_to_me",
						jsonObject.getString("comment_to_me"));
				resMap.put("comment_to_he",
						jsonObject.getString("comment_to_he"));
				resMap.put("work", jsonObject.getString("work"));
				resMap.put("work_att", jsonObject.getString("work_att"));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 服务订单详情
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static Map<String, String> employUserDetail(String strId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employUserDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("employ_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("shop_id", jsondata.getString("shop_id"));
				resMap.put("type", jsondata.getString("type"));
				resMap.put("card_type", jsondata.getString("card_type"));
				resMap.put("status", jsondata.getString("status"));
				resMap.put("button_word", jsondata.getString("button_word"));
				resMap.put("button_status", jsondata.getString("button_status"));
				resMap.put("days", jsondata.getString("days"));
				resMap.put("user_id", jsondata.getString("user_id"));
				resMap.put("username", jsondata.getString("username"));
				resMap.put("avatar", jsondata.getString("avatar"));
				resMap.put("cash", jsondata.getString("cash"));
				resMap.put("delivery_deadline",
						jsondata.getString("delivery_deadline")
								.substring(0, 10));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 服务订单详情-服务内容
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static Map<String, String> employServiceDetail(String strId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employServiceDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("employ_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("title", jsondata.getString("title"));
				resMap.put("desc", jsondata.getString("desc"));
				resMap.put("employ_att", jsondata.getString("employ_att"));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 服务订单详情-作品内容
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static Map<String, String> employWorkDetail(String strId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employWorkDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("employ_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				if (StrFormat.formatStr(jsondata.getString("work"))) {
					JSONObject jsonwork = new JSONObject(
							jsondata.getString("work"));
					resMap.put("desc", jsonwork.getString("desc"));
				} else {
					resMap.put("desc", " ");
				}

				resMap.put("work_att", jsondata.getString("work_att"));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 服务订单详情-评价内容
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static Map<String, String> employCommentDetail(String strId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employCommentDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("employ_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("type", jsondata.getString("type"));
				resMap.put("comment_to_me", jsondata.getString("comment_to_me"));
				resMap.put("comment_to_he", jsondata.getString("comment_to_he"));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 处理雇佣
	 * 
	 * @param strId
	 * @param strType
	 *            1：雇主取消 2：威客接受 3：威客拒绝
	 * @param context
	 */
	public static String dealEmploy(String strId, String strType,
			Context context) {
		String str = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/dealEmploy?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("type", strType);
		params.put("employ_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				str = resCode;
			} else {
				str = resMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return str;
	}

	/**
	 * 验收作品
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static String acceptEmployWork(String strId, Context context) {
		String res = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/acceptEmployWork";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));
			multipart.addPart("employ_id",
					new StringBody(strId, Charset.forName("UTF-8")));

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				res = resCode;
			} else {
				res = resMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 雇佣维权
	 * 
	 * @param strId
	 * @param intType
	 * @param strDesc
	 * @param context
	 * @return
	 */
	public static String employRights(String strId, int intType,
			String strDesc, Context context) {
		String res = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employRights";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));
			multipart.addPart("employ_id",
					new StringBody(strId, Charset.forName("UTF-8")));
			multipart.addPart("type",
					new StringBody("" + intType, Charset.forName("UTF-8")));
			multipart.addPart("desc",
					new StringBody(strDesc, Charset.forName("UTF-8")));

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				res = resCode;
			} else {
				res = resMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	public static String[] employEvaluate(String strId, String intType,
			String strDesc, float speed, float quality, float attitude,
			Context context) {
		String res[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/employEvaluate";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("employ_id", strId));
		params.add(new BasicNameValuePair("type", intType));
		params.add(new BasicNameValuePair("comment", strDesc));
		params.add(new BasicNameValuePair("speed_score", "" + speed));
		params.add(new BasicNameValuePair("quality_score", "" + quality));
		if (quality > 0) {
			params.add(new BasicNameValuePair("attitude_score", "" + attitude));
		}
		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			res[0] = jsonObject.getString("code");
			res[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 威客投稿
	 * 
	 * @param strId
	 * @param strDesc
	 * @param strFile
	 * @param context
	 */
	public static void workEmployCreate(String strId, String strDesc,
			String strFile, Context context) {
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/workEmployCreate";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("employ_id", strId));
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("file_id", strFile));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
				((Activity) context).finish();
				TestEvent event = new TestEvent();
				event.setOrderDetail(true);
				EventBus.getDefault().post(event);
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 附件上传
	 * 
	 * @param file_id
	 * @param context
	 * @return
	 */
	public static String fileUpload(String file_id, Context context) {
		String string = null;
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/fileUpload";
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			MultipartEntity multipart = new MultipartEntity();
			multipart.addPart("file", new FileBody(new File(file_id)));
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));

			String strJson = httpUtils.httpPost(urlString, multipart);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				string = jsondata.getString("id");
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return string;
	}
}
